﻿using Coolzon.Realtime.Contracts;
using Coolzon.Realtime.Contracts.CommandData;
using Coolzon.Realtime.ServerApp.Common;
using System.Collections.Generic;

namespace Coolzon.Realtime.ServerApp.ServiceImplement
{
    public class ReqCheckClientStatusHandler : BaseHandler<ReqCheckClientsStatusData, RespCheckClientsStatusData>
    {
        public ReqCheckClientStatusHandler()
            : base(RESPONSE_SCENARIO.CONNENT, RESPONSE_COMMAND.CONNECT.CHECKCLIENTSTATUS)
        {
        }

        protected override RespCheckClientsStatusData OnHandle(Request request, ReqCheckClientsStatusData requestData)
        {
            RespCheckClientsStatusData responseData = new RespCheckClientsStatusData();
            responseData.IsPass = true;
            DomainInstance domain = ServerData.Instance.GetDomain(request.Header.DomainId);
            if (domain != null)
            {
                List<Client> clients = domain.GetClientList();
                foreach (var client in clients)
                {
                    if(!CacheDataUtil.IsLiveTimeout(client) && client.Status != requestData.Status)
                    {
                        responseData.IsPass = false;
                        break;
                    }
                }
            }
            else
            {
                responseData.IsPass = false;
            }
            return responseData;
        }
    }
}
